package usbcali.edu.co.pqrs.presentation.backingBeans;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import usbcali.edu.co.pqrs.exceptions.ZMessManager;
import usbcali.edu.co.pqrs.modelo.MotReclXTpSol;
import usbcali.edu.co.pqrs.modelo.MotivoReclamacion;
import usbcali.edu.co.pqrs.modelo.TipoSolicitudPqr;
import usbcali.edu.co.pqrs.modelo.dto.MotReclXTpSolDTO;
import usbcali.edu.co.pqrs.modelo.dto.MotivoReclamacionDTO;
import usbcali.edu.co.pqrs.modelo.dto.TipoSolicitudPqrDTO;
import usbcali.edu.co.pqrs.presentation.businessDelegate.IBusinessDelegatorView;
import usbcali.edu.co.pqrs.utilities.FacesUtils;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
@ManagedBean
@ViewScoped
public class MotReclXTpSolView implements Serializable {
	private static final long serialVersionUID = 1L;

	private Long motivoReclamacion;
	private Long tipoSolicitudPqr;
	private SelectOneMenu txtTipoSolicitud;
	private SelectOneMenu txtMotivoReclamacion;
	private Map<String, String> losItemsTipoSolicitud = new HashMap<String, String>();
	private Map<String, String> losItemsMotivoReclamacion = new HashMap<String, String>();

	private InputText txtIdMotRecl_MotivoReclamacion;
	private InputText txtIdTpSolPqr_TipoSolicitudPqr;
	private InputText txtIdMotReclXTpSol;
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnBack;
	private CommandButton btnClear;
	private List<MotReclXTpSolDTO> data;
	private MotReclXTpSolDTO selectedMotReclXTpSol;
	private MotReclXTpSol entity;
	private boolean showDialog;
	List<MotivoReclamacionDTO> data2;
	List<TipoSolicitudPqrDTO> data3;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;

	private SelectItem[] manufacturerOptions;
	String manufacturers[] = { "A", "R" };

	public MotReclXTpSolView() {
		super();
		setManufacturerOptions(createFilterOptions(manufacturers));
	}

	public CommandButton getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(CommandButton btnBack) {
		this.btnBack = btnBack;
	}

	public MotReclXTpSol getEntity() {
		return entity;
	}

	public void setEntity(MotReclXTpSol entity) {
		this.entity = entity;
	}

	private SelectItem[] createFilterOptions(String[] data) {
		SelectItem[] options = new SelectItem[data.length + 1];

		options[0] = new SelectItem("", "Seleccionar");
		for (int i = 0; i < data.length; i++) {
			options[i + 1] = new SelectItem(data[i], data[i]);
		}

		return options;
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			MotReclXTpSolDTO motReclXTpSolDTO = (MotReclXTpSolDTO) e
					.getObject();

			if (txtIdMotRecl_MotivoReclamacion == null) {
				txtIdMotRecl_MotivoReclamacion = new InputText();
			}

			txtIdMotRecl_MotivoReclamacion.setValue(motReclXTpSolDTO
					.getIdMotRecl_MotivoReclamacion());

			if (txtIdTpSolPqr_TipoSolicitudPqr == null) {
				txtIdTpSolPqr_TipoSolicitudPqr = new InputText();
			}

			txtIdTpSolPqr_TipoSolicitudPqr.setValue(motReclXTpSolDTO
					.getIdTpSolPqr_TipoSolicitudPqr());

			if (txtMotivoReclamacion == null) {
				txtMotivoReclamacion = new SelectOneMenu();
			}
			txtMotivoReclamacion.setValue(motReclXTpSolDTO
					.getIdMotRecl_MotivoReclamacion());

			if (txtTipoSolicitud == null) {
				txtTipoSolicitud = new SelectOneMenu();
			}

			txtTipoSolicitud.setValue(motReclXTpSolDTO
					.getIdTpSolPqr_TipoSolicitudPqr());

			if (txtIdMotReclXTpSol == null) {
				txtIdMotReclXTpSol = new InputText();
			}

			txtIdMotReclXTpSol.setValue(motReclXTpSolDTO.getIdMotReclXTpSol());

			Long idMotReclXTpSol = FacesUtils.checkLong(txtIdMotReclXTpSol);
			entity = businessDelegatorView.getMotReclXTpSol(idMotReclXTpSol);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedMotReclXTpSol = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedMotReclXTpSol = null;

		if (txtIdMotRecl_MotivoReclamacion != null) {
			txtIdMotRecl_MotivoReclamacion.setValue(null);
			txtIdMotRecl_MotivoReclamacion.setDisabled(true);
		}

		if (txtIdTpSolPqr_TipoSolicitudPqr != null) {
			txtIdTpSolPqr_TipoSolicitudPqr.setValue(null);
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(true);
		}

		if (txtIdMotReclXTpSol != null) {
			txtIdMotReclXTpSol.setValue(null);
			txtIdMotReclXTpSol.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		return "";
	}

	public String action_back() {

		return "initialMenu.xhtml?faces-redirect=true";
	}

	public void listener_txtId() {
		try {
			Long idMotReclXTpSol = FacesUtils.checkLong(txtIdMotReclXTpSol);
			entity = (idMotReclXTpSol != null) ? businessDelegatorView
					.getMotReclXTpSol(idMotReclXTpSol) : null;
		} catch (Exception e) {
			entity = null;
		}

		if (entity == null) {
			txtIdMotRecl_MotivoReclamacion.setDisabled(false);
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
			txtIdMotReclXTpSol.setDisabled(false);
			btnSave.setDisabled(false);
		} else {
			txtIdMotRecl_MotivoReclamacion.setValue(entity
					.getMotivoReclamacion().getIdMotRecl());
			txtIdMotRecl_MotivoReclamacion.setDisabled(false);
			txtIdTpSolPqr_TipoSolicitudPqr.setValue(entity
					.getTipoSolicitudPqr().getIdTpSolPqr());
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
			txtIdMotReclXTpSol.setValue(entity.getIdMotReclXTpSol());
			txtIdMotReclXTpSol.setDisabled(true);
			btnSave.setDisabled(false);

			if (btnDelete != null) {
				btnDelete.setDisabled(false);
			}
		}
	}

	public String action_edit(ActionEvent evt) {
		selectedMotReclXTpSol = (MotReclXTpSolDTO) (evt.getComponent()
				.getAttributes().get("selectedMotReclXTpSol"));
		txtIdMotRecl_MotivoReclamacion.setValue(selectedMotReclXTpSol
				.getIdMotRecl_MotivoReclamacion());
		txtIdMotRecl_MotivoReclamacion.setDisabled(false);
		txtIdTpSolPqr_TipoSolicitudPqr.setValue(selectedMotReclXTpSol
				.getIdTpSolPqr_TipoSolicitudPqr());
		txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
		txtIdMotReclXTpSol.setValue(selectedMotReclXTpSol.getIdMotReclXTpSol());
		txtIdMotReclXTpSol.setDisabled(true);
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedMotReclXTpSol == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new MotReclXTpSol();

			MotivoReclamacion motivoReclamacionItem = businessDelegatorView
					.getMotivoReclamacion(motivoReclamacion);
			TipoSolicitudPqr tipoSolicitudPqrItem = businessDelegatorView
					.getTipoSolicitudPqr(tipoSolicitudPqr);

			entity.setMotivoReclamacion(motivoReclamacionItem);

			entity.setTipoSolicitudPqr(tipoSolicitudPqrItem);

			businessDelegatorView.saveMotReclXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			data = businessDelegatorView.getDataMotReclXTpSol();
			RequestContext.getCurrentInstance().update("form:listaTDo");
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Long idMotReclXTpSol = new Long(
						selectedMotReclXTpSol.getIdMotReclXTpSol());
				entity = businessDelegatorView
						.getMotReclXTpSol(idMotReclXTpSol);
			}

			entity.setMotivoReclamacion((FacesUtils
					.checkLong(txtIdMotRecl_MotivoReclamacion) != null) ? businessDelegatorView
					.getMotivoReclamacion(FacesUtils
							.checkLong(txtIdMotRecl_MotivoReclamacion)) : null);
			entity.setTipoSolicitudPqr((FacesUtils
					.checkLong(txtIdTpSolPqr_TipoSolicitudPqr) != null) ? businessDelegatorView
					.getTipoSolicitudPqr(FacesUtils
							.checkLong(txtIdTpSolPqr_TipoSolicitudPqr)) : null);
			businessDelegatorView.updateMotReclXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedMotReclXTpSol = (MotReclXTpSolDTO) (evt.getComponent()
					.getAttributes().get("selectedMotReclXTpSol"));

			Long idMotReclXTpSol = new Long(
					selectedMotReclXTpSol.getIdMotReclXTpSol());
			entity = businessDelegatorView.getMotReclXTpSol(idMotReclXTpSol);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Long idMotReclXTpSol = FacesUtils.checkLong(txtIdMotReclXTpSol);
			entity = businessDelegatorView.getMotReclXTpSol(idMotReclXTpSol);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deleteMotReclXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedMotReclXTpSol = (MotReclXTpSolDTO) (evt.getComponent()
					.getAttributes().get("selectedMotReclXTpSol"));

			Long idMotReclXTpSol = new Long(
					selectedMotReclXTpSol.getIdMotReclXTpSol());
			entity = businessDelegatorView.getMotReclXTpSol(idMotReclXTpSol);
			businessDelegatorView.deleteMotReclXTpSol(entity);
			data.remove(selectedMotReclXTpSol);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(Long idMotReclXTpSol,
			Long idMotRecl_MotivoReclamacion, Long idTpSolPqr_TipoSolicitudPqr)
			throws Exception {
		try {
			businessDelegatorView.updateMotReclXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("MotReclXTpSolView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

	public InputText getTxtIdMotRecl_MotivoReclamacion() {
		return txtIdMotRecl_MotivoReclamacion;
	}

	public void setTxtIdMotRecl_MotivoReclamacion(
			InputText txtIdMotRecl_MotivoReclamacion) {
		this.txtIdMotRecl_MotivoReclamacion = txtIdMotRecl_MotivoReclamacion;
	}

	public InputText getTxtIdTpSolPqr_TipoSolicitudPqr() {
		return txtIdTpSolPqr_TipoSolicitudPqr;
	}

	public void setTxtIdTpSolPqr_TipoSolicitudPqr(
			InputText txtIdTpSolPqr_TipoSolicitudPqr) {
		this.txtIdTpSolPqr_TipoSolicitudPqr = txtIdTpSolPqr_TipoSolicitudPqr;
	}

	public InputText getTxtIdMotReclXTpSol() {
		return txtIdMotReclXTpSol;
	}

	public void setTxtIdMotReclXTpSol(InputText txtIdMotReclXTpSol) {
		this.txtIdMotReclXTpSol = txtIdMotReclXTpSol;
	}

	public List<MotReclXTpSolDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataMotReclXTpSol();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<MotReclXTpSolDTO> motReclXTpSolDTO) {
		this.data = motReclXTpSolDTO;
	}

	public MotReclXTpSolDTO getSelectedMotReclXTpSol() {
		return selectedMotReclXTpSol;
	}

	public void setSelectedMotReclXTpSol(MotReclXTpSolDTO motReclXTpSol) {
		this.selectedMotReclXTpSol = motReclXTpSol;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public void onEdit(org.primefaces.event.RowEditEvent event) {

		try {

			MotReclXTpSolDTO m = (MotReclXTpSolDTO) event.getObject();

			entity = null;
			entity = businessDelegatorView.getMotReclXTpSol(m
					.getIdMotReclXTpSol());

			motivoReclamacion = m.getIdMotRecl_MotivoReclamacion();
			tipoSolicitudPqr = m.getIdTpSolPqr_TipoSolicitudPqr();

			MotivoReclamacion motivoReclamacionItem = businessDelegatorView
					.getMotivoReclamacion(motivoReclamacion);

			TipoSolicitudPqr tipoSolicitudPqrItem = businessDelegatorView
					.getTipoSolicitudPqr(tipoSolicitudPqr);

			entity.setTipoSolicitudPqr(tipoSolicitudPqrItem);
			entity.setMotivoReclamacion(motivoReclamacionItem);

			businessDelegatorView.updateMotReclXTpSol(entity);

			/*
			 * try { List<TipoSolicitudPqrDTO> data = businessDelegatorView
			 * .getDataTipoSolicitudPqr();
			 * 
			 * for (int i = 0; i < data.size(); i++) {
			 * losItemsTipoSolicitud.put(data.get(i).getDescTpSol(), data
			 * .get(i).getIdTpSolPqr() + "");
			 * 
			 * } } catch (Exception e) { e.printStackTrace(); }
			 * 
			 * try { List<MotivoReclamacionDTO> data2 = businessDelegatorView
			 * .getDataMotivoReclamacion();
			 * 
			 * for (int i = 0; i < data2.size(); i++) {
			 * losItemsMotivoReclamacion.put(data2.get(i)
			 * .getDescripcionMotRecl(), data2.get(i) .getIdMotRecl() + "");
			 * 
			 * } } catch (Exception e) { e.printStackTrace(); }
			 * 
			 * businessDelegatorView.updateMotReclXTpSol(entity);
			 */

			data = businessDelegatorView.getDataMotReclXTpSol();

			RequestContext.getCurrentInstance().update("form:listaTDo");

			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void onCancel(org.primefaces.event.RowEditEvent event) {
		FacesMessage msg = new FacesMessage("TipoEstado Cancelled",
				((MotReclXTpSolDTO) event.getObject()).getIdMotReclXTpSol()
						+ "");

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public SelectItem[] getManufacturerOptions() {
		return manufacturerOptions;
	}

	public void setManufacturerOptions(SelectItem[] manufacturerOptions) {
		this.manufacturerOptions = manufacturerOptions;
	}

	public String[] getManufacturers() {
		return manufacturers;
	}

	public void setManufacturers(String[] manufacturers) {
		this.manufacturers = manufacturers;
	}

	public Long getMotivoReclamacion() {
		return motivoReclamacion;
	}

	public void setMotivoReclamacion(Long motivoReclamacion) {
		this.motivoReclamacion = motivoReclamacion;
	}

	public Long getTipoSolicitudPqr() {
		return tipoSolicitudPqr;
	}

	public void setTipoSolicitudPqr(Long tipoSolicitudPqr) {
		this.tipoSolicitudPqr = tipoSolicitudPqr;
	}

	public Map<String, String> getLosItemsTipoSolicitud() {
		try {
			if (data3 == null) {
				getData3();
				for (int i = 0; i < data3.size(); i++) {
					losItemsTipoSolicitud.put(data3.get(i).getDescTpSol(),
							data3.get(i).getIdTpSolPqr() + "");

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsTipoSolicitud;
	}

	public void setLosItemsTipoSolicitud(
			Map<String, String> losItemsTipoSolicitud) {
		this.losItemsTipoSolicitud = losItemsTipoSolicitud;
	}

	public Map<String, String> getLosItemsMotivoReclamacion() {
		try {
			if (data2 == null) {
				getData2();
				for (int i = 0; i < data2.size(); i++) {
					losItemsMotivoReclamacion.put(data2.get(i)
							.getDescripcionMotRecl(), data2.get(i)
							.getIdMotRecl() + "");

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsMotivoReclamacion;
	}

	public void setLosItemsMotivoReclamacion(
			Map<String, String> losItemsMotivoReclamacion) {
		this.losItemsMotivoReclamacion = losItemsMotivoReclamacion;
	}

	public SelectOneMenu getTxtTipoSolicitud() {
		return txtTipoSolicitud;
	}

	public void setTxtTipoSolicitud(SelectOneMenu txtTipoSolicitud) {
		this.txtTipoSolicitud = txtTipoSolicitud;
	}

	public SelectOneMenu getTxtMotivoReclamacion() {
		return txtMotivoReclamacion;
	}

	public void setTxtMotivoReclamacion(SelectOneMenu txtMotivoReclamacion) {
		this.txtMotivoReclamacion = txtMotivoReclamacion;
	}

	public List<MotivoReclamacionDTO> getData2() {
		try {
			if (data2 == null) {
				data2 = businessDelegatorView.getDataMotivoReclamacion();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data2;
	}

	public void setData2(List<MotivoReclamacionDTO> data2) {
		this.data2 = data2;
	}

	public List<TipoSolicitudPqrDTO> getData3() {
		try {
			if (data3 == null) {
				data3 = businessDelegatorView.getDataTipoSolicitudPqr();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data3;
	}

	public void setData3(List<TipoSolicitudPqrDTO> data3) {
		this.data3 = data3;
	}

}
